<?php
	require_once '../lib/controller.php';

	//继承controller的后台控制器类
	class backend extends controller
	{
		var $get_privilege_id = '';
		var $post_privilege_id = '';
		var $operate_memo = '';
		var $account_info = array();
		var $before = array( 'get' => array( 'check' => false ), 'post' => array( 'check' => false ) );
		var $after = array( 'get' => array( 'log' => false ), 'post' => array( 'log' => false ) );

		//记录后台操作，以get_privilege_id、post_privilege_id为记录点，不同的privilege_id代表不同的操作
		function log()
		{
			$privilege_id = $this->{$this->method.'_privilege_id'};

			if ( preg_match( '!^\d+$!', $privilege_id ) && isset( $this->account_info['id'] ) )
			{
				$operate_log = load( 'biz.operate_log' );
				//$operate_log->add( array( 'account_id' => $this->account_info['id'], 'memo' => $this->operate_memo, 'privilege_id' => $privilege_id ) );
			}
		}

		//判断用户是否已登陆和具有相应get_privilege_id或post_privilege_id的操作权限
		function check()
		{
			$result = false;
			$check = $this->{$this->method.'_privilege_id'};

			if ( $check != '' )
			{
				$account = load( 'biz.account' );
				$info = $account->login_info();

				if ( $info['status'] == 0 && $info['username'] != '' )
				{
					$this->account_info = $info;

					if ( $account->check( $info['account_id'], $check ) )
					{
						$result = true;
					}
					else
					{
						$this->prompt( '对不起，你没有该操作的权限', array( array( 'url' => 'login.php', 'name' => '重新登录' ), array( 'url' => config( 'server.HTTP_REFERER' ), 'name' => '返回上一页' ) ) );
					}
				}
				else
				{
					header( 'location: ' . 'login.php' );
				}
			}
			else
			{
				$result = true;
			}

			return $result;
		}

		//把错误信息数组转化为给前台view使用的格式
		function transform( $info, $url )
		{
			$data = array();
			if ( is_array( $info ) )
			{
				foreach( $info as $message ) $data[] = array( 'url' => $url, 'name' => $message );
			}

			if ( empty( $data ) )
			{
				$data[] = array( 'url' => $_SERVER['HTTP_REFERER'], 'name' => 'unknown' );
			}
			return $data;
		}
	}
?>